﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using System.Web.Services;
using System.Data;
namespace AirPlane
{
    public partial class XepLichBayChoKhachHang : System.Web.UI.Page
    {
        protected ChuyenBayBUS cb = new ChuyenBayBUS();
        protected VeMayBayBUS ve = new VeMayBayBUS();
        protected DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["islogin"] == null || (bool)Session["islogin"] == false || Session["type"].ToString() != "1")
            {

                Response.Redirect("TrangChu.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    datepickerdepart_chuyenBay.Text = string.Format("{0:MM/dd/yyyy}", DateTime.Now);
                    ddlChuyenBay.DataSource = cb.layThongTinChuyenBay();
                    ddlChuyenBay.DataBind();
                    lbThongBao.Text = "";
                }
            }
           
        }
        [WebMethod]
        public static string layChuyenBay(string ngayDi)
        {
            string s = "";
            DataTable dt = new DataTable();
            ChuyenBayBUS cb1 = new ChuyenBayBUS();
            dt = cb1.layThongTinChuyenBay(ngayDi);
            foreach (DataRow dr in dt.Rows)
            {
                s += "<option value='" + dr["ID_chuyenBay"].ToString() + "'>" + dr["tenChuyenbay"].ToString() + "</option>";
            }
            return s;
        }

        

        protected void Button1_Click(object sender, EventArgs e)
        {
            fillData(ddlChuyenBay.SelectedValue.ToString());
        }

        protected void gwDanhSachVe_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            // Neu du lieu cu bang du lieu moi thi khong thuc hien update xuong co so du lieu
            if (e.NewValues == e.OldValues)
            {
                e.Cancel = true;
            }
            else
            {
                string maVe = ((Label)gwDanhSachVe.Rows[e.RowIndex].Cells[0].FindControl("Label2")).Text;
                string viTri = ((TextBox)gwDanhSachVe.Rows[e.RowIndex].Cells[0].FindControl("txtViTri")).Text;
                int n = 0;
                n = ve.capNhatVeMayBay(maVe, viTri);
               
                gwDanhSachVe.EditIndex = -1;
                fillData(ddlChuyenBay.SelectedValue.ToString());
            }
        }

        protected void gwDanhSachVe_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gwDanhSachVe.EditIndex = e.NewEditIndex;
            fillData(ddlChuyenBay.SelectedValue.ToString());
        }

        protected void gwDanhSachVe_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gwDanhSachVe.EditIndex = -1;
            fillData(ddlChuyenBay.SelectedValue.ToString());
        }

        public void fillData(string maChuyenBay)
        {
            lbThongBao.Text = "";
            dt = ve.layDanhSachVe(maChuyenBay);
            gwDanhSachVe.DataSource = null;
            gwDanhSachVe.DataBind();
            if (dt.Rows.Count > 0)
            {
                gwDanhSachVe.DataSource = dt;
                gwDanhSachVe.DataBind();
            }
            else
            {
                lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Không Tìm Thấy Vé Nào! Xin Vui Lòng Chọn Lại Ngày Đi Hoặc Chọn Lại Chuyến Bay.</span>";
            }
        }

        protected void gwDanhSachVe_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gwDanhSachVe.PageIndex = e.NewPageIndex;
            fillData(ddlChuyenBay.SelectedValue.ToString());

        }

    }
}